package com.lefevre.cms.web.action.common;


import com.lefevre.cms.bean.setting.SystemSetting;
import com.lefevre.cms.service.setting.SettingService;
import com.lefevre.cms.service.theme.ThemeService;
import com.lefevre.cms.utils.WebUtil;
import com.lefevre.cms.web.action.AccessDeviceUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 默认消息页
 */
@Controller
public class MessageAction {

    @Resource
    AccessDeviceUtil accessDeviceUtil;
    @Resource
    SettingService settingService;
    @Resource(name = "themeServiceImpl")
    private ThemeService themeService;

    @RequestMapping("/message")
    public String execute(ModelMap model,
                          HttpServletRequest request, HttpServletResponse response) throws Exception {
        SystemSetting systemSetting = settingService.findSystemSetting_cache();

        boolean isAjax = WebUtil.submitDataMode(request);

        if (isAjax) {
            if (systemSetting.getCloseSite() > 1) {//如果关闭网站
                response.setStatus(503);
            }
            WebUtil.writeToWeb(systemSetting.getCloseSitePrompt(), "html", response);
            return null;
        } else {
            if (systemSetting.getCloseSite() > 1) {//如果关闭网站
                model.addAttribute("message", systemSetting.getCloseSitePrompt());
            }

            //当前模板使用的目录
            String dirName = themeService.findTemplateDir_cache();
            return "/templates/" + dirName + "/" + accessDeviceUtil.accessDevices(request) + "/message";
        }

    }
}
